package com.hotau.account.mode.payment;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hotau.HotauActivity;
import com.hotau.R;
import com.hotau.account.PaymentAdvanceManageFragment;
import com.hotau.account.UnBindPaymentAdvanceFragment;

/**
 * 预付费卡Mode适配器
 * 
 * @author liujia (liujia9234@126.com)
 * @data @2011-12-11
 * @version 1.0
 *          <ul>
 *          <li>1.0 创建</li>
 *          </ul>
 */
public class DetailAdapter extends BaseAdapter {

	private List<AdvanceEntity> data = new ArrayList<AdvanceEntity>();

	private Context ctx;
	private LayoutInflater mInflater;
	private PaymentAdvanceManageFragment parentFrament;

	public DetailAdapter(Context context, Handler handler,PaymentAdvanceManageFragment parentFrament) {
		this.ctx = context;
		mInflater = LayoutInflater.from(ctx);
		this.parentFrament = parentFrament;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return data.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return data.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		final LinearLayout layout = new LinearLayout(ctx);
		LayoutInflater vi = (LayoutInflater) ctx
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		Object obj = getItem(position);
		if (obj instanceof AdvanceEntity) {
			final AdvanceEntity entiey = (AdvanceEntity) obj;
			vi.inflate(R.layout.account_payment_advance_list_layout, layout,true);
			String cardInfo = entiey.getCardInfo();
			((TextView) layout
					.findViewById(R.id.account_payment_advance_list_tv_cardinfo))
					.setText(cardInfo);
			Button payment_advance_btn_unbind = (Button) layout
					.findViewById(R.id.account_payment_advance_btn_unbind);
			payment_advance_btn_unbind
					.setOnClickListener(new OnClickListener() {
						@Override
						public void onClick(View v) {
							// TODO Auto-generated method stub
							// 解绑
							HotauActivity csmbpActivity = (HotauActivity) ctx;
							Bundle bundle = new Bundle();
							bundle.putSerializable("parentFrament", parentFrament);
							bundle.putString("cardNo", entiey.getCardNo());
							Fragment f = new UnBindPaymentAdvanceFragment();
							f.setArguments(bundle);
							csmbpActivity.pushFragment("解绑预付卡", f);
//							DialogUtil.showDialog(ctx, "", "确定要解除"
//									+ entiey.getCardInfo() + "在绑定?",
//									new Executable() {
//										@Override
//										public void execute() {
//											// TODO Auto-generated method stub
//											final HNAPAYActivity csmbpActivity = (HNAPAYActivity) ctx;
//											HttpRequestAsynTask payment_advance_type_task = new HttpRequestAsynTask(
//													csmbpActivity) {
//												@Override
//												protected void doPostExecute(
//														String result) {
//													try {
//														JSONObject data = new JSONObject(
//																result);
//														if (data != null
//																&& data
//																		.getInt("result") == 1) {
//															// 解绑卡成功
//															// DialogUtil.showDialog(csmbpActivity,
//															// "", "解绑卡成功");
//															hander
//																	.sendEmptyMessage(PaymentAdvanceManageFragment.ON_LOAD);
//														} else {
//															// 解绑卡卡失败
//															DialogUtil
//																	.showDialog(
//																			csmbpActivity,
//																			"",
//																			data
//																					.getString("errorMsg"));
//														}
//													} catch (JSONException e) {
//														// TODO Auto-generated
//														// catch block
//														DialogUtil.showDialog(
//																csmbpActivity,
//																"",
//																"解绑卡返回数据出错 ");
//													}
//												}
//											};
//											StringBuilder data = new StringBuilder();
//											data.append("Form:token="
//													+ MemberService
//															.getToken(ctx));
//											data.append(";cardNo="
//													+ entiey.getCardNo());
//											data.append(";payPwd=" + "");
//											data.append(";terminalCode=" + "");
//											payment_advance_type_task
//													.execute(
//															HNAPAYConstants.ACCOUNT_UNBIND_CARD_INFO,
//															data.toString(),
//															HttpUtil.UTF8_ENCODING);
//										}
//									}, new Executable() {
//										@Override
//										public void execute() {
//											// TODO Auto-generated method stub
//										}
//									});
						}
					});
		}

		return layout;
	}

	/**
	 * 增加数据
	 * 
	 * @param name
	 * @param date
	 * @param text
	 */
	@SuppressWarnings("unchecked")
	public void addItem(AdvanceEntity entity) {
		data.add(entity);
	}

	public void clearData() {
		data.clear();
	}

}
